<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\CourseSource;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CourseSourceController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new CourseSource(['class']), function (Grid $grid) {
            $grid->model()->orderByDesc('id');
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('class.name', '分类名称');
            $grid->column('cover')->image(' ', 50);
            $grid->column('file', '视频')->display('预览')->modal(function ($modal){
                $modal->title('预览');
                $modal->icon('');

                $file = $this->file;

                return <<<HTML
<div><video src="{$file}" controls="controls" autoplay width="100%"></video></div>
HTML;

            });
            $grid->column('sort');
            $grid->column('status')->switch();
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();

                $filter->equal('id')->width(3);
                $filter->like('name')->width(3);
                $filter->equal('course_source_class_id', '分类')->select(\App\Models\CourseSourceClass::orderByDesc('sort')->pluck('name', 'id')->all())->width(3);
                $filter->between('created_at')->datetime()->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new CourseSource(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('course_source_class_id');
            $show->field('type');
            $show->field('cover');
            $show->field('file');
            $show->field('sort');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new CourseSource(), function (Form $form) {
            $form->display('id');
            $form->text('name')->required();
            $form->select('course_source_class_id', '素材分类')->options(\App\Models\CourseSourceClass::orderByDesc('sort')->pluck('name', 'id')->all())->required();
            $form->hidden('type')->value('video');
            $form->image('cover')->uniqueName()->autoUpload()->saveFullUrl()->required();
            $form->file('file', '视频')->chunked()->chunkSize(1024 * 5)->maxSize(1024 * 2 * 1000)->autoUpload()->uniqueName()->saveFullUrl()->required();
            $form->number('sort')->default(0);
            $form->radio('status')->options(['禁用', '正常'])->default(1);

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
